package com.cxrj.zhsq.interfaceManage.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cxrj.zhsq.civilianManage.service.CivilianService;
import com.cxrj.zhsq.civilianManage.service.RelevantDepartmentsService;
import com.cxrj.zhsq.utils.IpUtil;

@Controller
@RequestMapping(value = "/interface")
public class InterfaceCivilianController {

	@Resource
	private CivilianService civilianService;
	
	@Resource
	private RelevantDepartmentsService relevantDepartmentsService;

	/**
	 * 查询便民服务
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectCivilian", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectCivilian(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = civilianService.seleteCivilianByStatus();
			resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/upload/zhsq_xg/");
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 根据code查询便民服务
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectCivilianCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectCivilianCode(String code, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = civilianService.selectByCivilianCode(code, "1");
			resultMap.put("path", "http://" + IpUtil.getIpAddr(request) + ":" + request.getServerPort() + "/upload/zhsq_xg/");
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}

		return resultMap;
	}

	/**
	 * 根据code查询法律服务并分页
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectLegalService", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectLegalService(String code, String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = civilianService.selectLegalService(code, dataIndex, pageCount);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 根据id查询法律服务
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectLegalServiceId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectLegalServiceId(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = civilianService.selectLegalServiceById(id);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}
	
	/**
	 * 根据code查询法律服务并分页
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectRelevant", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectRelevant(String code, String dataIndex, String pageCount, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = relevantDepartmentsService.selectRd(code, dataIndex, pageCount);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}
	
	/**
	 * 根据id查询法律服务
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectRelevantId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Map<String, Object> selectRelevantId(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = relevantDepartmentsService.selectRdById(id);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}
}
